//
//  TeamDetailController.m
//  InformationFoot
//
//  Created by 魔曦 on 2018/7/16.
//  Copyright © 2018年 MacBook. All rights reserved.
//

#import "TeamDetailController.h"
#import "TeamDetailTableViewCell.h"
#import "MXBattleModel.h"
#import "MXEventBasicPanelModel.h"

@interface TeamDetailController ()<UITableViewDelegate,UITableViewDataSource>
{
    int teamIndex;
}
@property (nonatomic , strong) NSMutableArray * dataArray ;

@property (nonatomic , strong) MXEventBasicPanelModel * basicPanelModel ;

@property (nonatomic , strong) MXBattleModel * battleModel ;

@end

static NSString *const cellDefinser = @"TeamDetailTableViewCell";

@implementation TeamDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    teamIndex = 0;
    self.title = self.firstHeadViewFirstName;
    self.firstHeadView.hidden = NO;
    self.secondHeadView.hidden = YES;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [_tableView registerNib:[UINib nibWithNibName:@"TeamDetailTableViewCell" bundle:nil] forCellReuseIdentifier:cellDefinser];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    [self setImage];
    [self getNetworkData];
    mx_weakify(self) ;
    if (self.dataArray.count == 0) {
        [self.bgButton setTitle:@"暂无数据" forState:UIControlStateNormal];
    }else{
        if (teamIndex == 0) {
            if (!self.basicPanelModel.homeVs.battle.count) {
                [self.bgButton setTitle:@"" forState:UIControlStateNormal];
            }else{
                self.bgButton.hidden = YES;
            }
        } else {
            if (!self.basicPanelModel.awayVs.battle.count) {
               [self.bgButton setTitle:@"" forState:UIControlStateNormal];
            }else{
                self.bgButton.hidden = YES;
            }
        }
    }

}
- (void)setImage{
    [self.firstHeadViewFirstImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_firstHeadViewFirstImgStr]] placeholderImage:Image(@"saishi_huilogo")] ;
    [self.firstHeadViewSecondImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_firstHeadViewSecondImgStr]] placeholderImage:Image(@"saishi_huilogo")] ;
    [self.secondHeadViewFirstImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_secondHeadViewFirstImgStr]] placeholderImage:Image(@"saishi_huilogo")] ;
    [self.secondHeadViewSecondImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_secondHeadViewSecondImgStr]] placeholderImage:Image(@"saishi_huilogo")] ;
    self.firstHeadViewFirstLabel.text = self.firstHeadViewFirstName;
    self.firstHeadViewSecondLabel.text = self.firstHeadViewSecondName;
    self.secondHeadViewFirstLabel.text = self.secondHeadViewFirstName;
    self.secondHeadViewSecondLabel.text = self.secondHeadViewSecondName;

    
}
#pragma mark - network
- (void)getNetworkData {

    NSString *timeStr = [MXLJUtil getNowDateTimeString];
    
    NSString * matchID = [NSString stringWithFormat:@"%ld",(long)_matchId] ;
    
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];

    [parameters setObject:matchID forKey:@"matchId"] ;
    [parameters setObject:timeStr forKey:@"time"];
    NSMutableDictionary *dic= [MXLJUtil sortedDictionary:parameters];
    NSString *url = [NSString stringWithFormat:@"%@%@", SERVER_HOST, MXEventBasicPanelPATH];
    [SVProgressHUD showWithStatus:@"正在加载..."];
    
    mx_weakify(self);
    [[WebManager sharedManager]requestWithMethod:POST WithUrl:url WithParams:dic WithSuccessBlock:^(NSDictionary *dic) {
        [weakSelf.tableView.mj_header endRefreshing] ;
        
        if ([dic[@"code"] isEqualToString:@"0"]) {
            
            weakSelf.basicPanelModel = [MXEventBasicPanelModel mj_objectWithKeyValues:dic[@"data"]] ;
            
            if (!weakSelf.basicPanelModel.matchId) {
                self.bgButton.hidden = NO;

                [SVProgressHUD showErrorWithStatus:@"暂无数据"];
                [self.bgButton setTitle:@"暂无数据" forState:UIControlStateNormal];

            } else {
                if (teamIndex == 0) {
                    self.bgButton.hidden = NO;

                    if (!self.basicPanelModel.homeVs.battle.count) {
                        [self.bgButton setTitle:@"暂无数据" forState:UIControlStateNormal];

                    } else {
                        weakSelf.bgButton.hidden = YES;
                        
                    }
                } else {
                    self.bgButton.hidden = NO;

                    if (!self.basicPanelModel.awayVs.battle.count) {
                        [self.bgButton setTitle:@"暂无数据" forState:UIControlStateNormal];
                    } else {
                        weakSelf.bgButton.hidden = YES;
                    }
                }
//                weakSelf.removeUpdataBlock() ;
//                weakSelf.dataRefreshing(weakSelf.basicPanelModel) ;
            }
            
            
      
            
            
            
            [SVProgressHUD dismiss];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [weakSelf.tableView reloadData];
                
                
            });
            
            
        } else {
            [SVProgressHUD showErrorWithStatus:dic[@"msg"]];
            if (!weakSelf.basicPanelModel.matchId) {
                self.bgButton.hidden = NO;

                [self.bgButton setTitle:dic[@"msg"] forState:UIControlStateNormal];

            }
        }
        
    } WithFailureBlock:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing] ;
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        if (!weakSelf.basicPanelModel.matchId) {
            self.bgButton.hidden = NO;
            [self.bgButton setTitle:@"网络错误" forState:UIControlStateNormal];
        }
    }] ;
    
}
#pragma mark TableView delegate/datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (teamIndex == 0) {
        if (!self.basicPanelModel.homeVs.battle.count) {
            return 0;
        }else{
            return 1;
        }
    } else {
        if (!self.basicPanelModel.awayVs.battle.count) {
            return 0;
        }else{
            return 1;
        }
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.basicPanelModel.matchId) {
        if (teamIndex == 0) {
            if (self.basicPanelModel.homeVs.battle.count) {
                return self.basicPanelModel.homeVs.battle.count ;
            }else{
                return 0;
            }
        }else{
                if (self.basicPanelModel.awayVs.battle.count) {
                    return self.basicPanelModel.awayVs.battle.count;
                }else{
                    return 0;
                }
        }
    }else{
        return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return scaleWithSize(60);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    TeamDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellDefinser];
    if (!cell) {
        cell = [[TeamDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellDefinser];
    }
    
    if (self.basicPanelModel.matchId) {
        if (teamIndex == 0) {
            self.battleModel = self.basicPanelModel.homeVs.battle[indexPath.row] ;
        }else{
            self.battleModel = self.basicPanelModel.awayVs.battle[indexPath.row] ;
        }
            
        cell.battleModel = self.battleModel ;
            
    }
    
    cell.selectionStyle = 0 ;
    return cell;
    
    
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (IBAction)firstHeadView:(UIButton *)sender {
    self.secondHeadView.hidden = NO;
    self.firstHeadView.hidden = YES;
    teamIndex = 1;
    self.title = self.firstHeadViewSecondName;
    [self getNetworkData];
    
    
    
}
- (IBAction)secondHeadView:(UIButton *)sender {
    self.firstHeadView.hidden = NO;
    self.secondHeadView.hidden = YES;
    teamIndex = 0;
    self.title = self.firstHeadViewFirstName;
    [self getNetworkData];
    
}


@end
